define([ "knockout", "jquery", "framework/js/constants/constants", "baseService", "baseModel", "baseLogger", "text!build.fingerprint", "extensions/extension", "promise" ], function (ko, $, Constants, BaseService, BaseModel, BaseLogger, BuildFingerPrint, ExtensionOverride) { "use strict"; var baseModel = BaseModel.getInstance(); window.console.log = console.log = BaseLogger.info; var module = baseModel.QueryParams.get("module"), context = baseModel.QueryParams.get("context"), queryMap = baseModel.QueryParams.get(); var vm = { getBaseModel: function () { return baseModel; } }, userSegmentMap = { "CORPADMIN": "admin" }, isDashboardSet = ko.observable(true), roles = ["CorporateAdminChecker", "CorporateAdminMaker", "Viewer", "Checker", "Maker", "AdminChecker", "AdminMaker", "Administrator", "RetailUser", "KSV_EBANK", "KSV_PTNT_NHDT", "KSV_PDVKH", "KSV_PGS_XLTS", "GDV_EBANK", "NV_PTNT_NHDT", "NV_PDVKH", "NV_PGS_XLTS"]; Constants.buildFingerPrint = JSON.parse(BuildFingerPrint); function isExist(value) { return value == -1 ? false : true; } function isMaker(roleList) { return isExist(roleList.indexOf("Maker")) || isExist(roleList.indexOf("CorporateAdminMaker")) || isExist(roleList.indexOf("AdminMaker")) || isExist(roleList.indexOf("GDV_EBANK")) || isExist(roleList.indexOf("NV_PTNT_NHDT")) || isExist(roleList.indexOf("NV_PDVKH")) || isExist(roleList.indexOf("NV_PGS_XLTS")); } function isChecker(roleList) { return isExist(roleList.indexOf("Checker")) || isExist(roleList.indexOf("CorporateAdminChecker")) || isExist(roleList.indexOf("AdminChecker")) || isExist(roleList.indexOf("KSV_PGS_XLTS")) || isExist(roleList.indexOf("KSV_PDVKH")) || isExist(roleList.indexOf("KSV_PTNT_NHDT")) || isExist(roleList.indexOf("KSV_EBANK")); } function computeRole(role, roleList) { if (role === "Viewer") { return "viewer"; } else if (role === "Checker" || role === "CorporateAdminChecker" || role === "AdminChecker" || role === "KSV_PGS_XLTS" || role === "KSV_PDVKH" || role === "KSV_PTNT_NHDT" || role === "KSV_EBANK") { // if (!isMaker(roleList)) { // return "checker"; // } else { // return "approver"; // } return "approver"; } else if (role === "Maker" || role === "CorporateAdminMaker" || role === "AdminMaker" || role === "GDV_EBANK" || role === "NV_PTNT_NHDT" || role === "NV_PDVKH" || role === "NV_PGS_XLTS") { return "maker"; } else if (role === "Administrator") { if (isChecker(roleList)) { return "approver"; } else if (isMaker(roleList)) { return "maker"; } } return "dashboard"; } function computeDashboardRole(module, roles) { if (!module) { return computeRole(roles[0], roles); } else { if ($.inArray(module, ["maker", "viewer", "approver"]) > -1) { return module; } else { return computeRole(roles[0], roles); } } } function loadCSS(context) { var mainCSS = (Constants.brandPath ? Constants.brandPath + "/" : "") + context + "/css/main" + ((!Constants.brandPath && Constants.buildFingerPrint.timeStamp) ? ("." + Constants.buildFingerPrint.timeStamp) : ""); var widgetCSS = (Constants.brandPath ? Constants.brandPath + "/" : "") + context + "/css/widgets" + ((!Constants.brandPath && Constants.buildFingerPrint.timeStamp) ? ("." + Constants.buildFingerPrint.timeStamp) : ""); $("body").addClass("page-is-changing"); require.undef("css!" + mainCSS); require.undef("css!" + widgetCSS); $("link[rel=stylesheet][href$=\"" + mainCSS + ".css\"]").remove(); $("link[rel=stylesheet][href$=\"" + widgetCSS + ".css\"]").remove(); require(["css!" + mainCSS, "css!" + widgetCSS], function () { $("body").removeClass("page-is-changing"); $("div.cd-logo").removeClass("cd-logo"); }); } loadCSS("index"); function getLocalJSON(module, data, dashboardRole, resolve) { var landingContext = userSegmentMap[Constants.userSegment] || Constants.jsonContext; loadCSS(landingContext); require(["json!../../framework/json/landings/" + landingContext], function (jsonData) { var currentModule = null; if (queryMap && queryMap.homeComponent && queryMap.homeModule) { currentModule = { homeComponent: queryMap.homeComponent, application: queryMap.homeModule }; } else { currentModule = jsonData[module]; if (currentModule) { jsonData[module].application = module; } else if (module === "user-login-configuration") { currentModule = { homeComponent: "configuration-base", application: "user-login-configuration" }; } else if (module === "system-configuration") { currentModule = { homeComponent: "system-configuration-home", application: "system-configuration" }; } else { currentModule = { // homeComponent: "error", application: "login" }; } } vm.dashboardRole = dashboardRole; resolve({ currentModule: currentModule, vmMetaData: jsonData, userData: data }); }); } function computeContext(segment) { if (segment === "CORPADMIN") { return "corp-admin"; } else if (segment === "RETAIL") { return "retail"; } else if (segment === "CORP") { return "corporate"; } else if (segment === "ADMIN") { return "admin"; } return "index"; } function computeSegment(roles, context) { var rolesString = roles.toString().toLowerCase(); var extUserType = ExtensionOverride.evaluateSegment(roles); if (context) { return "ANON"; } else if (extUserType) { return extUserType; } else if (rolesString.indexOf("corporateadminmaker") > -1 || rolesString.indexOf("corporateadminchecker") > -1) { return "CORPADMIN"; } else if (rolesString.indexOf("retailuser") > -1) { return "RETAIL"; } else if (rolesString.indexOf("corporateuser") > -1) { return "CORP"; } else if (rolesString.indexOf("administrator") > -1) { return "ADMIN"; } return "ANON"; } function changeUserSegment(userSegment, userData, module) { $(window).off(); isDashboardSet(false); vm.isUserDataSet(false); ko.tasks.runEarly(); var array = roles.filter(function (element) { return userData.userProfile && userData.userProfile.roles.indexOf(element) !== -1; }); var dashboardRole = computeDashboardRole(module, array); module = module || dashboardRole; Constants.userSegment = userSegment; Constants.jsonContext = ExtensionOverride.evaluateContext(Constants.userSegment, roles) || computeContext(Constants.userSegment); vm.userInfoPromise = new Promise(function (resolve) { getLocalJSON(module, userData, dashboardRole, resolve); isDashboardSet(true); }); } function deleteAllCookies() { var cookies = document.cookie.split(";"); for (var i = 0; i < cookies.length; i++) { var cookie = cookies[i]; var eqPos = cookie.indexOf("="); var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; } } function setConstants(entity, segment, brand, timezoneOffset, jsonContext) { Constants.currentEntity = entity || Constants.defaultEntity; Constants.userSegment = segment; Constants.brandPath = brand; Constants.jsonContext = jsonContext; Constants.timezoneOffset = timezoneOffset; } function onError(jqXHR, resolve, module) { var segment = computeSegment([], context); var jsonContext = ExtensionOverride.evaluateContext(segment, roles) || computeContext(segment); setConstants("", "ANON", null, null, jsonContext); if (jqXHR && jqXHR.status === 400) { if (jqXHR.responseJSON.message.code === "DIGX_UM_042") { module = "change-password"; console.log('---c p'); } else { baseModel.showMessages(jqXHR); module = "error"; } } // if (module == null) { // //deleteAllCookies(); // // window.location.href = window.location.origin + "/index.html?module=login"; // return; // } // if (window.location.pathname!=="/index.html"){ // window.location.href = window.location.origin + "/index.html?module=login"; // return; // } if (module == 'login') { deleteAllCookies(); logOutDBAuthDef(); window.localStorage.clear(); } module = module || "login"; getLocalJSON(module, null, module, resolve); } function logOutDBAuthDef() { if (navigator.serviceWorker && navigator.serviceWorker.controller) { navigator.serviceWorker.controller.postMessage("logout"); } var options = { url: "session", success: function () { // window.location.href = window.location.origin + "/index.html?module=login"; } }; BaseService.getInstance().remove(options); }; function systemConfiguration(module, userData, dashboardRole, resolve) { BaseService.getInstance().fetch({ url: "configurations/base/dayoneconfig/properties/SYSTEM_CONFIGURATION" }).then(function (data) { if (data.configResponseList[0].propertyValue === "false") { module = "system-configuration"; } getLocalJSON(module, userData, dashboardRole, resolve); }); } function onSuccess(data, resolve) { var dashboardRole; var array = roles.filter(function (element) { return data.userProfile.roles.indexOf(element) !== -1; }); vm.roles = array; var segment = computeSegment(data.userProfile.roles, context); if (!context) { vm.currentRole.push(segment + "~" + array[0]); } dashboardRole = computeDashboardRole(module, array); if (!data.firstLoginFlowDone) { module = "user-login-configuration"; } else { module = module || dashboardRole; } var jsonContext = ExtensionOverride.evaluateContext(segment, roles) || computeContext(segment); setConstants(data.userProfile.homeEntity, segment, data.branding, data.userProfile.timeZoneDTO.offset, jsonContext); if (Constants.userSegment === "ADMIN" && data.userProfile.roles.indexOf("AuthAdmin") !== -1) { systemConfiguration(module, data, dashboardRole, resolve); } else { getLocalJSON(module, data, dashboardRole, resolve); } } function setUserInformation() { return new Promise(function (resolve) { //module != null && if (module !== "login") { BaseService.getInstance().fetch({ url: "me", showMessage: false }).then(function (data) { onSuccess(data, resolve); }, function (jqXHR) { //dung thay doi onError(jqXHR, resolve, module); //khong tim thay me, thi login lai //onError(null, resolve, "login"); }); } else { onError(null, resolve, "login"); } }); } $(document).ready(function () { baseModel.registerElement("dashboard", "core"); baseModel.registerElement("message-box", "core"); baseModel.registerElement("modal-window"); ko.utils.extend(vm, { userInfoPromise: setUserInformation(), isUserDataSet: ko.observable(false), queryMap: queryMap || null, changeUserSegment: changeUserSegment, isDashboardSet: isDashboardSet, currentRole: ko.observableArray(), computeRole: computeRole, dashboardRole: null }); $(".message-box").remove(); ko.applyBindings(vm); }); });